//
//  DiaryListVc.swift
//  YourName
//
//  Created by 李朋 on 2017/5/19.
//  Copyright © 2017年 李朋. All rights reserved.
//

import UIKit
import MJRefresh

fileprivate let cellId = "diaryCellId"
class DiaryListVc: BaseVc {
    
    var diaryId = 10
    
    let diary = DiaryModel()
    
    var diaryLastId:Int {
        get {
            return diary.dataArray.last?.diaryId ?? 0
        }
    }
    
    fileprivate lazy var tableView: UITableView = {
        let tableView = UITableView(frame: .zero, style: .plain)
        tableView.register(UITableViewCell.self, forCellReuseIdentifier: cellId)
        tableView.tableFooterView = UIView()
        tableView.dataSource = self
        return tableView
    }()
    
    
     override func viewDidLoad() {
        super.viewDidLoad()
        title = "日记列表"
        setupUI()
        let name = NSNotification.Name(rawValue: "justTest")
        NotificationCenter.default.addObserver(self, selector: #selector(reloadTableview), name: name, object: nil)
    }
    
     @objc fileprivate func reloadTableview(n:Notification) {
        if let text = n.object as? String {
            let model = Diary()
            model.diary = text
            diary.dataArray.insert(model, at: 0)
            tableView.reloadData()
        }
    }
    
    deinit {
        DLog("1")
        NotificationCenter.default.removeObserver(self)
    }

}

extension DiaryListVc {
    
    @objc fileprivate func loadData() {
        diary.loadData(diaryId: Int(MAXINTERP))
        tableView.mj_header.endRefreshing()
        tableView.reloadData()
    }
    
    @objc fileprivate func loadMoreData() {
        diary.loadMoreData(diaryId: diaryLastId)
        tableView.mj_footer.endRefreshing()
        tableView.reloadData()
    }

    fileprivate func setupUI() {
        tableView.frame = view.bounds
        tableView.mj_header = MJRefreshNormalHeader(refreshingTarget: self, refreshingAction: #selector(loadData))
        tableView.mj_footer = MJRefreshBackNormalFooter(refreshingTarget: self, refreshingAction: #selector(loadMoreData))
        
        /// MARK: 不加[weak self]会循环引用
        
//        tableView.mj_header = MJRefreshNormalHeader(refreshingBlock: { [weak self] in
//            self?.loadData()
//        })
//        tableView.mj_footer = MJRefreshBackNormalFooter(refreshingBlock: { [weak self] in
//            self?.loadMoreData()
//        })
        view.addSubview(tableView)
        navigationItem.rightBarButtonItem = UIBarButtonItem(title: "创建", style: .done, target: self, action: #selector(addDiaryData))
    }
    
    @objc fileprivate func addDiaryData() {
        present(CreatDiaryVc(), animated: true, completion: nil)
    }

}

extension DiaryListVc: UITableViewDataSource {

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return diary.dataArray.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: cellId, for: indexPath)
        let model = diary.dataArray[indexPath.row]
        cell.textLabel?.text = model.diary
        return cell
    }
}




